package org.example.myleet.p1218;

public class Solution {
    public int longestSubsequence(int[] arr, int difference) {
        int base = 20000;
        int maxLen = 0;
        int last;
        int curBase;
        int lastBase;
        //表加速寻找
        int[] map = new int[2 * base + 4];
        for (int cur : arr) {
            last = cur - difference;
            curBase = cur + base;
            lastBase = last + base;
            if (map[lastBase] > 0) {
                //动态规划
                map[curBase] = Math.max(map[curBase], map[lastBase] + 1);
            } else {
                map[curBase] = 1;
            }
            maxLen = Math.max(maxLen, map[curBase]);
        }
        return maxLen;
    }
}
